#include "stdafx.h"
#include <XDraw.h>

XRenderPack::XRenderPack(XHash hash)
{
	Hash = hash;
	Vertices = 0;

	Texture = NULL;
	
	VertexBuffer = 0;
	IndexBuffer = 0;

	Color = XColor(255, 255, 255, 255);

	Translation.X = 0;
	Translation.Y = 0;

	Rotation = 0;
	Scale = 1;
	LineMode = false;

	_initBuffers();
}

XRenderPack::~XRenderPack()
{
	_freeBuffers();
}
void XRenderPack::SetColor(XColor color)
{
	Color = color;
}

void XRenderPack::SetTranslation(XVector translation)
{
	Translation = translation;
}

void XRenderPack::SetRotation(float degree)
{
	Rotation = degree;
}

void XRenderPack::SetScale(float scale)
{
	Scale = scale;
}

void XRenderPack::SetTexture(XTexture* tex, XVertex* vertices, int num)
{
	Texture = tex;
	TextureVertex.clear();

	for(int i=0; i<num; i++)
	{
		TextureVertex.push_back(vertices[i]);
	}

	_updateBuffers();

}
void XRenderPack::SetVertex(XVertex* vertices, int num)
{
	Vertex.clear();

	for(int i=0; i<num; i++)
	{
		Vertex.push_back(vertices[i]);
	}

	_updateBuffers();
}

void XRenderPack::SetLineMode()
{
	LineMode = true;
}

void XRenderPack::SetTriangleMode()
{
	LineMode = false;
}

XHash XRenderPack::GetHash()
{
	return Hash;
}

XColor XRenderPack::GetColor()
{
	return Color;
}

XVector XRenderPack::GetTranslation()
{
	return Translation;
}

float XRenderPack::GetRotation()
{
	return Rotation;
}

float XRenderPack::GetScale()
{
	return Scale;
}


void XRenderPack::_initBuffers()
{
	_freeBuffers();

	XVertex vData[4];
	unsigned int iData[4];

	glGenBuffers( 1, &VertexBuffer);
	glBindBuffer( GL_ARRAY_BUFFER, VertexBuffer); 
	glBufferData( GL_ARRAY_BUFFER, 4 * sizeof(XVertex), vData, GL_DYNAMIC_DRAW ); 

	glGenBuffers( 1, &IndexBuffer); 
	glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, IndexBuffer); 
	glBufferData( GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(unsigned int), iData, GL_DYNAMIC_DRAW ); 

	glBindBuffer( GL_ARRAY_BUFFER, NULL );
	glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, NULL );

}

void XRenderPack::_updateBuffers()
{
	bool isRebuffNeed = false;

	if(TextureVertex.size() > Vertex.size() || TextureVertex.size() == 0)
	{
		if(Vertices != Vertex.size())
			isRebuffNeed = true;

		Vertices = Vertex.size();
	}
	else
	{
		if(Vertices != TextureVertex.size())
			isRebuffNeed = true;

		Vertices = TextureVertex.size();
	}

	unsigned int* iData = new unsigned int[Vertices];

	for(int i=0; i<Vertices; i++)
	{
		iData[i] = i;
	}

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBuffer);
	if(isRebuffNeed)
		glBufferData( GL_ELEMENT_ARRAY_BUFFER, Vertices * sizeof(unsigned int), iData, GL_DYNAMIC_DRAW); 
	else
		glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, Vertices * sizeof(unsigned int), iData);

	glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, NULL );

	delete[] iData;

	if(TextureVertex.size() > 0)
	{
		XVertexData* vData = new XVertexData[Vertices];

		for(int i=0; i<Vertices; i++)
		{
			vData[i].Position = Vertex[i];
			vData[i].TextureCoord = TextureVertex[i];
		}

		glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
		glBufferData(GL_ARRAY_BUFFER, Vertices * sizeof(XVertexData), vData, GL_DYNAMIC_DRAW);
		glBindBuffer( GL_ARRAY_BUFFER, NULL );

		delete[] vData;
	}
	else
	{
		XVertex* vData = new XVertex [Vertices];
		for(int i=0; i<Vertices; i++)
		{
			vData[i] = Vertex[i];
		}

		glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
		if(isRebuffNeed)
			glBufferData(GL_ARRAY_BUFFER, Vertices * sizeof(XVertex), vData, GL_DYNAMIC_DRAW);
		else
			glBufferSubData(GL_ARRAY_BUFFER, 0, Vertices * sizeof(XVertex), vData);

		glBindBuffer( GL_ARRAY_BUFFER, NULL );
	}

	
}

void XRenderPack::_freeBuffers()
{
	if(VertexBuffer != 0)
	{
		glDeleteBuffers(1, &VertexBuffer);
		VertexBuffer = 0;
	}

	if(IndexBuffer != 0)
	{
		glDeleteBuffers(1, &IndexBuffer);
		IndexBuffer = 0;
	}
}